﻿using System;
using System.Data;
using System.Linq;
using NHibernate;
using NHibernate.Linq;

namespace TimeKeeper.Infrastructure
{
    public static class SessionExtensions
    {
        public static void QueryInTransaction<T>(this ISession session, Action<IQueryable<T>> action)
        {
            using (var tx = session.BeginTransaction(IsolationLevel.ReadCommitted))
            {
                action(session.Query<T>());
                tx.Commit();
            }
        }

        public static void DoInTransaction(this ISession session, Action<ISession> action)
        {
            using (var tx = session.BeginTransaction(IsolationLevel.ReadCommitted))
            {
                action(session);
                tx.Commit();
            }
        }

         public static void QueryInTransaction<T>(this IStatelessSession session, Action<IQueryable<T>> action)
         {
             using (var tx = session.BeginTransaction(IsolationLevel.ReadCommitted))
             {
                 action(session.Query<T>());
                 tx.Commit();
             }
         }

         public static void DoInTransaction(this IStatelessSession session, Action<IStatelessSession> action)
         {
             using (var tx = session.BeginTransaction(IsolationLevel.ReadCommitted))
             {
                 action(session);
                 tx.Commit();
             }
         }
    }
}